<script setup lang="ts">
import {computed, reactive, ref} from 'vue';
import { $t } from '@/locales';
import { loginModuleRecord } from '@/constants/app';
import { useRouterPush } from '@/hooks/common/router';
import { useAntdForm, useFormRules } from '@/hooks/common/form';
import { useAuthStore } from '@/store/modules/auth';
import {getVerifyCode} from "@/service/api/common";

defineOptions({
  name: 'PwdLogin'
});

const authStore = useAuthStore();
const { toggleLoginModule } = useRouterPush();
const { formRef, validate } = useAntdForm();

interface FormModel {
  username: string;
  password: string;
  uid: string;
  verify_code: string;
}

const model: FormModel = reactive({
  username: 'admin',
  password: 'qiyi1987@',
  uid: '',
  verify_code: ''
});

const rules = computed<Record<keyof FormModel, App.Global.FormRule[]>>(() => {
  // inside computed to make locale reactive, if not apply i18n, you can define it without computed
  const { formRules } = useFormRules();

  return {
    username: formRules.username,
    password: formRules.pwd,
    uid: formRules.uid,
    verify_code: formRules.code
  };
});

const verifyCodeImg = ref('');
const verifyCode = async () => {
  const res = await getVerifyCode();
  model.uid = res.data.uid;
  verifyCodeImg.value = `data:image/png;base64,${res.data.image}`;
  console.log(verifyCodeImg.value);
}
verifyCode();

async function handleSubmit() {
  await validate();
  await authStore.login(model);
}
</script>

<template>
  <AForm ref="formRef" :model="model" :rules="rules">
    <AFormItem name="userName">
      <AInput v-model:value="model.username" size="large" :placeholder="$t('page.login.common.userNamePlaceholder')" />
    </AFormItem>
    <AFormItem name="password">
      <AInputPassword
        v-model:value="model.password"
        size="large"
        :placeholder="$t('page.login.common.passwordPlaceholder')"
      />
    </AFormItem>
    <a-row :gutter="24">
      <a-col :span="12">
        <img :src="verifyCodeImg" @click="verifyCode" alt="验证码" style="width: 100%; height: 40px; object-fit: cover" />
      </a-col>
      <a-col :span="12">
        <AFormItem name="verify_code">
        <AInput
          v-model:value="model.verify_code"
          size="large"
          :placeholder="$t('page.login.common.codePlaceholder')"
        />
        </AFormItem>
      </a-col>
    </a-row>
    <ASpace direction="vertical" size="large" class="w-full">
      <div class="flex-y-center justify-between">
        <ACheckbox>{{ $t('page.login.pwdLogin.rememberMe') }}</ACheckbox>
        <AButton type="text" @click="toggleLoginModule('reset-pwd')">
          {{ $t('page.login.pwdLogin.forgetPassword') }}
        </AButton>
      </div>
      <AButton type="primary" block size="large" shape="round" :loading="authStore.loginLoading" @click="handleSubmit">
        {{ $t('common.confirm') }}
      </AButton>
      <div class="flex-y-center justify-between">
        <AButton class="h-34px flex-1" block @click="toggleLoginModule('code-login')">
          {{ $t(loginModuleRecord['code-login']) }}
        </AButton>
        <div class="w-12px"></div>
        <AButton class="h-34px flex-1" block @click="toggleLoginModule('register')">
          {{ $t(loginModuleRecord.register) }}
        </AButton>
      </div>
    </ASpace>
  </AForm>
</template>

<style scoped></style>
